/**
 * 
 */
package org.o14x.alpha.ui.views.folder.columns;

import java.io.File;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

/**
 * The name column helper.
 * 
 * @author Olivier DANGREAUX
 */
public class NameColumnHelper extends ColumnHelper {
	/* (non-Javadoc)
	 * @see org.o14x.alpha.ui.views.folder.columns.ColumnHelper#createTableColumn(org.eclipse.swt.widgets.Table, org.eclipse.swt.widgets.Listener)
	 */
	@Override
	public TableColumn createTableColumn(Table parent, Listener listener) {
		TableColumn tableColumn = super.createTableColumn(parent, listener);
		tableColumn.setWidth(200);
		return tableColumn;
	}

	@Override
	public int compare(File file1, File file2) {
		return file1.getName().toLowerCase().compareTo(file2.getName().toLowerCase());
	}

	@Override
	public Image getColumnImage(File file) {
		Image image = null;

		if (file.isDirectory()) {
			image = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
		} else {
			int dotIndex = file.getName().lastIndexOf(".");
			if (dotIndex != -1) {
				String extension = file.getName().substring(dotIndex);
				Program launcher = Program.findProgram(extension);
				if (launcher != null) {
					ImageData imgData = launcher.getImageData();
					image = new Image(null, imgData);
				}
			}

			if (image == null) {
				image = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE);
			}
		}

		return image;
	}

	@Override
	public String getColumnText(File file) {
		return file.getName();
	}

	@Override
	public String getTitle() {
		return "Name";
	}

}
